package com.asion.vmovie.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.asion.vmovie.R;
import com.asion.vmovie.activities.XiLieActivity;
import com.asion.vmovie.adapters.XiLieRecyclerAdapter;
import com.asion.vmovie.beans.SeriesBean;
import com.asion.vmovie.ui.MVPContract.XiLieContract;
import com.asion.vmovie.ui.MVPModel.XiLieModel;
import com.asion.vmovie.ui.MVPPresenter.XiLiePresenter;
import com.asion.vmovie.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by asion on 16-10-18.
 */

public class XiLieFragment extends BaseFragment<XiLiePresenter, XiLieModel> implements XiLieContract.View {

    @BindView(R.id.xilie_search)
    ImageView xilieSearch;
    @BindView(R.id.xielie_recycler)
    RecyclerView xielieRecycler;
    @BindView(R.id.refresh)
    SwipeRefreshLayout refresh;
    private XiLieRecyclerAdapter xiLieRecyclerAdapter;

    public static XiLieFragment newInstance() {
        return new XiLieFragment();
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.xilie_fragment;
    }

    @Override
    public void initPresenter() {
        mPresenter.setVM(this, mModel);
        mPresenter.getXiLieDatas(1,10);
    }

    @Override
    protected void initView() {
        refresh.setColorSchemeColors(Color.BLUE,Color.CYAN,Color.GRAY);
        startProgressDialog();
        xiLieRecyclerAdapter = new XiLieRecyclerAdapter(getContext());
        xielieRecycler.setItemAnimator(new DefaultItemAnimator());
        xielieRecycler.setLayoutManager(new LinearLayoutManager(getContext()));
        xielieRecycler.setAdapter(xiLieRecyclerAdapter);

        refresh.setOnRefreshListener(()->{
            page =0;
            xiLieRecyclerAdapter.removeAll();
            new Handler().postDelayed(()->refresh.setRefreshing(false),2000);
        });

        xiLieRecyclerAdapter.setOnAddClickListener((view, dataBean) -> {
            ToastUtil.showShort("点击了收藏");
        });

        xiLieRecyclerAdapter.setOnItemClickListener(dataBean -> {
            Bundle bundle = new Bundle();
            bundle.putString("seriesid",dataBean.getSeriesid());
            XiLieActivity.showAction(getActivity(),bundle);
        });

        xielieRecycler.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                if(dy > 0){
                    EventBus.getDefault().post("hide");
                }else{
                    EventBus.getDefault().post("show");
                }
                LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
                int itemCount = layoutManager.getItemCount();
                int lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition();
                if (!loading&&itemCount<(lastVisibleItemPosition+2)) {
                    mPresenter.getXiLieDatas(++page,10);
                    loading=true;
                }
            }
        });
    }

    private int page =1;
    private boolean loading =false;

    @OnClick({R.id.xilie_search})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.xilie_search:
                EventBus.getDefault().post("search");
                break;
        }
    }

    @Override
    public void showDataSuccess(SeriesBean seriesBean) {
        stopProgressDialog();
        xiLieRecyclerAdapter.addAll(seriesBean.getData());
        loading = false;
    }

    @Override
    public void showDataError(Throwable throwable) {
        ToastUtil.showShort("网络请求失败");
    }

}
